/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webcamtracking;

import java.awt.Color;

/**
 *
 * @author James
 */
public class Pixel {

    private int x;
    private int y;
    private int red;
    private int green;
    private int blue;
    private Color color;
    private int colorRGB;

    public Pixel(int color) {
        colorRGB = color;
        setColor(color);
    }

    public void setRGB(int red, int green, int blue) {
        setRed(red);
        setGreen(green);
        setBlue(blue);
    }
    public void setRGBCol(int color){
        this.colorRGB = color;    
    }
    
    public void setRed(int red){
        if (red > 255) {
            this.red = 255;
        } else if (red < 0) {
            this.red = 255;
        }else{
            this.red = red;
        }
    }
    
    public void setGreen(int green){
        if (green > 255) {
            this.green = 255;
        } else if (red < 0) {
            this.green = 0;
        }else{
            this.green = green;
        }
    }
    
    public void setBlue(int blue){
        if (blue > 255) {
            this.blue = 255;
        } else if (blue < 0) {
            this.blue = 0;
        }else{
            this.blue = blue;
        }
    }

    public void setColor(int c) {        
        red = (c & 0x00ff0000) >> 16;
        green = (c & 0x0000ff00) >> 8;
        blue = c & 0x000000ff;
        color = new Color(red, green, blue);
    }
    
    public Color getColor(){
        color = new Color(red,green,blue);
        return color;
    }
    
    public int getRGB(){
        return colorRGB;
    }

    public int getRed() {
        return red;
    }

    public int getGreen() {
        return green;
    }

    public int getBlue() {
        return blue;
    }
    
    public boolean isSimilar(int c, int threshold){
        
        int red1 = (colorRGB & 0x00ff0000) >> 16;
        int green1 = (colorRGB & 0x0000ff00) >> 8;
        int blue1 = colorRGB & 0x000000ff;
        
        int red2 = (c & 0x00ff0000) >> 16;
        int green2 = (c & 0x0000ff00) >> 8;
        int blue2 = c & 0x000000ff;
        
        int redDiff = red1 - red2;
        int greenDiff = green1 - green2;
        int blueDiff = blue1 - blue2;
        
        if (redDiff < 0 ){
            redDiff *= -1;
        }
        if (greenDiff < 0 ){
            greenDiff *= -1;
        }
        if (blueDiff < 0 ){
            blueDiff *= -1;
        }
        
        int threshVal = ((255/100)*threshold);
        
        if (redDiff <= threshVal &&
                blueDiff <= threshVal &&
                greenDiff <= threshVal){
            return true;
        }
        
        else{
            return false;
        }
    }
}
